using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Search : System.Web.UI.UserControl
{
    processProduct pProduct;
    PagedDataSource pDataSource;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string ProductName = Request.QueryString["ProductName"];
            // Response.Write(ProductTypeID.ToString());
            
            if (ProductName.ToString().Trim().Equals(""))
            {
                lblResult.Text = "No Item has found!";
            }              
            else
            {
                pProduct = new processProduct();
                pDataSource = new PagedDataSource();
                pDataSource.DataSource = pProduct.searchByName(ProductName).DefaultView;
                pDataSource.AllowPaging = true;
                pDataSource.PageSize = 8;
                pDataSource.CurrentPageIndex = 0;
                dtlProduct.DataSource = pDataSource;                
                dtlProduct.DataBind();
                string pages = null;
                for (int i = 0; i < pDataSource.PageCount; i++)
                {
                    pages = pages + "<a href='index.aspx?subpara=search&ProductName="+ProductName+"&page="+i+"'>" + (i + 1) + "</a>||";
                }
                lblPage.Text = pages;
                if (Request["page"] != null)
                {
                    int page = Int32.Parse(Request["page"]);
                    pDataSource.CurrentPageIndex = page;
                    dtlProduct.DataSource = pDataSource;
                    dtlProduct.DataBind();
                    //Label1.Text = page.ToString();
                } 
    
                //dtlProduct.DataSource = pProduct.searchByName(ProductName).Tables[0];
                //dtlProduct.DataKeyField = "ProductName";
                //dtlProduct.DataBind();
            }

            //
            if (dtlProduct.Items.Count == 0)
                lblResult.Text = "No Item has found!";
        }
    }

    protected void dtlProduct_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Label lblProductName = (Label)e.Item.FindControl("lblProductName");
        Label lblProductPrice = (Label)e.Item.FindControl("lblProductPrice");
        Label lblProductQuantity = (Label)e.Item.FindControl("lblProductQuantity");
        ImageButton ibtProductImage = (ImageButton)e.Item.FindControl("ibtProductImage");
        HyperLink hplProductDetail = (HyperLink)e.Item.FindControl("hplProductDetail");
        DataRowView row = (DataRowView)e.Item.DataItem;
        lblProductName.Text = row["ProductName"].ToString();
        lblProductPrice.Text = row["ProductPrice"].ToString();
        lblProductQuantity.Text = row["ProductQuantity"].ToString();
        ibtProductImage.ImageUrl = "Images/" + row["ProductImage"].ToString();
        //ibtProductImage.PostBackUrl = "index.aspx?subpara=productdetail&ProductID=" + ((DataRowView)e.Item.DataItem)["ProductID"].ToString();        
        hplProductDetail.NavigateUrl = "index.aspx?subpara=productdetail&ProductID=" + ((DataRowView)e.Item.DataItem)["ProductID"].ToString();       
    }
}
